package com.xuchen.zhenyang.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.Debt;
import com.xuchen.zhenyang.entity.User;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.enums.DebtTypeEnum;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.service.DebtService;
import com.xuchen.zhenyang.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("debt")
@RequiresPermissions("debt")
public class DebtController extends BaseController {


    @Autowired
    DebtService debtService;
    @Autowired
    UserService userService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "debt/debt-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, Debt myEntity, String params) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(Debt.class);
        }
        MyEntityWrapper<Debt> wrapper = new MyEntityWrapper<>(baseQuery, myEntity);
        wrapper.eq("debt_type").eq("user_id").timeBetween("bill_time");
        List<Debt> list = debtService.selectList(wrapper);
        return Result.success(PageHelper.freeTotal(), list);
    }

    @PostMapping("editText")
    @ResponseBody
    @RequestLog
    Result editText(Debt myEntity){
        debtService.updateById(myEntity);
        return Result.success();
    }

    @GetMapping("toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "debt/debt-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(Debt myEntity) {
        myEntity.setCreateUser(getSessionUserName());
        myEntity.setCreateTime(new Date());
        debtService.insert(myEntity);
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(Debt myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", debtService.selectById(myEntity));
        return "debt/debt-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(Debt myEntity) {
        debtService.updateById(myEntity);
        return Result.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result delete(Debt myEntity) {
        debtService.deleteById(myEntity);
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
        List<User> userList = userService.selectList(new EntityWrapper<User>().eq("status", StatusEnum.NORMAL.getId()).orderBy("user_name"));
        request.setAttribute("userList", userList);
        request.setAttribute("debtType", DebtTypeEnum.getMap());
    }
}
